<?php

namespace app\common\model;

use think\Db;
use think\Model;

class SettingRechargeRebate extends Model
{
    // 表名
    protected $name = 'setting_recharge_rebate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];
    

    



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function rebate($user, $amount) {
    	if(!$user['parent_path'] || $amount <= 0) {
    		return true;
	    }
	    
	    $rule = $this
		    ->find();
    	
    	if(!$rule || ($rule['pid'] == 0 && $rule['ppid'] == 0 && $rule['pppid'] == 0)) {
    		return true;
	    }
	    
	    $parents = array_reverse(explode(',', $user['parent_path']));
    	if(!$parents) {
    		return true;
	    }
	    
	    //上级返利
	    if(isset($parents[0]) && isset($rule['pid']) && $rule['pid'] * $amount > 0) {
	    	$pid = $parents[0];
	    	$pAmount = $rule['pid'] * $amount;
	    	$pres = $this->addAmountAndLog($pid, $pAmount, $user['id']);
	    	if(!$pres) {
	    		return false;
		    }
	    }
	
	    //上级返利
	    if(isset($parents[1]) && isset($rule['ppid']) && $rule['ppid'] * $amount > 0) {
		    $pid = $parents[1];
		    $pAmount = $rule['ppid'] * $amount;
		    $pres = $this->addAmountAndLog($pid, $pAmount, $user['id']);
		    if(!$pres) {
			    return false;
		    }
	    }
	
	    //上级返利
	    if(isset($parents[2]) && isset($rule['pppid']) && $rule['pppid'] * $amount > 0) {
		    $pid = $parents[2];
		    $pAmount = $rule['pppid'] * $amount;
		    $pres = $this->addAmountAndLog($pid, $pAmount, $user['id']);
		    if(!$pres) {
			    return false;
		    }
	    }
	    
	    return true;
    }
	
	
	/**
	 * 添加推荐人返利和流水
	 * @param $uid 上级UID
	 * @param $amount 金额
	 * @param $relationUid 充值用户
	 * @return bool
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    protected function addAmountAndLog($uid, $amount, $relationUid) {
    	$user = Db::name('user')->find($uid);
    	
    	//添加金额
	    $amountRes = Db::name('user')
		    ->where(['id' => $uid])
		    ->setInc('balance', $amount);
	    
	    if(!$amountRes) {
	    	$this->error = '添加推荐人返利出错';
	    	return false;
	    }
	    
	    $logModel = new FundsRecord();
	    $logRes = $logModel->addRecord($uid, $amount, $user['balance'], FundsRecord::ACCOUNT_TYPE_BALANCE, FundsRecord::OPE_TYPE_PLUS, FundsRecord::TYPE_REBATE, '','下级充值返利', $relationUid);
	    if(!$logRes) {
	    	$this->error = '添加推荐人返利资金记录出错';
	    	return false;
	    }
	    
	    return true;
    }
}
